package org.zero;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author yufa.wang (yufa.wang@ronganchina.com)
 * @since 2024/1/12
 */
public class Main {
    public static void main(String[] args) {


        // 保证程序不退出
        await0();
    }

    /**
     * 阻塞
     */
    private static void await0() {
        try {
            System.in.read();
        } catch (IOException ignored) {
        }
    }

    /**
     * 阻塞
     */
    private static void await1() {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        try {
            countDownLatch.await();
        } catch (InterruptedException ignored) {
        }
    }

    /**
     * 阻塞
     */
    private static void await2() {
        Object o = new Object();
        synchronized (o) {
            try {
                o.wait();
            } catch (InterruptedException ignored) {
            }
        }
    }

    /**
     * 线程休眠
     */
    private static void await3() {
        try {
            Thread.sleep(Long.MAX_VALUE);
            // TimeUnit.DAYS.sleep(Long.MAX_VALUE);
        } catch (InterruptedException ignored) {
        }
    }

    /**
     * 死循环
     */
    private static void await4() {
        while (true) {
           // 为防止cpu占用过高
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignored) {
            }
        }
    }

    /**
     *
     */
    private static void await5() {
        HoldProcessor holdProcessor = new HoldProcessor();
        holdProcessor.startAwait();
        Runtime.getRuntime()
                .addShutdownHook(new Thread(holdProcessor::stopAwait));
    }

    public static class HoldProcessor {
        private volatile boolean stopAwait = false;
        /**
         * Thread that currently is inside our await() method.
         */
        private volatile Thread awaitThread = null;

        /**
         * 开始等待
         */
        public void startAwait() {
            Thread awaitThread = new Thread(this::await, "hold-process-thread");
            awaitThread.setContextClassLoader(this.getClass().getClassLoader());
            awaitThread.setDaemon(false);
            awaitThread.start();
        }

        /**
         * 停止等待，退出程序，一般放在shutdown hook中执行
         *
         * @see Runtime#addShutdownHook(Thread)
         */
        public void stopAwait() {
            stopAwait = true;
            Thread t = awaitThread;
            if (null != t) {
                t.interrupt();
                try {
                    t.join(1000);
                } catch (InterruptedException ignored) {
                }
            }
        }

        private void await() {
            try {
                awaitThread = Thread.currentThread();
                while (!stopAwait) {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException ignored) {
                    }
                }
            } finally {
                awaitThread = null;
            }
        }
    }
}